<?php

namespace App\Containers\AppSection\Project\Tasks;

use App\Containers\AppSection\Project\Data\Repositories\ProjectRepository;
use App\Ship\Criterias\ThisProfileCriteria;
use App\Ship\Criterias\Where;
use App\Ship\Criterias\WhereIn;
use App\Ship\Parents\Tasks\Task;

class GetProjectsTask extends Task
{
    protected ProjectRepository $repository;

    public function __construct(ProjectRepository $repository)
    {
        $this->repository = $repository;
    }

    public function run()
    {
        return $this->repository->paginate();
    }

    public function thisProfile($profileId): self
    {
        $this->repository->pushCriteria(new ThisProfileCriteria($profileId));
        return $this;
    }

    public function notDrafted(): self
    {
        $this->repository
            ->unsetFieldsSearchableKeys('is_draft')
            ->pushCriteria(new Where('is_draft', '=', false));
        return $this;
    }

    public function filterByStatus($status): self
    {
        switch ($status) {
            case 'collecting' :
                $this->repository->pushCriteria(new Where('moderation_status_key', '=', 'projects_collecting'));
                break;
            case 'completed' :
                $this->repository->pushCriteria(new WhereIn('moderation_status_key', [
                    'projects_collecting_completed',
                    'projects_signing_stage',
                    'projects_payment_services',
                    'projects_payment_stage',
                    'projects_delay',
                    'projects_completed',
                    'projects_not_carried_out',
                    'projects_canceled',
                ]));
                break;
        }


        return $this;
    }
}
